#!/usr/bin/env bash

# ------------------------------------------------------------------------------
#
# Program: initdraft.sh
# Author:  Thiago Rossener
# Description: script to create an initial structure for my drafts.
#
# Usage: ./initdraft.sh [options] <draft name>
#
# Options:
#   -h, --help        output instructions
#   -c, --create      create post
#
# Alias: alias newdraft="bash ~/path/to/script/initdraft.sh"
#
# Example:
#   ./initdraft.sh -c How to replace strings with sed
#
# Important Notes:
#   - This script was created to generate new markdown files for my blog.
#
# ------------------------------------------------------------------------------


# ------------------------------------------------------------------------------
# | VARIABLES                                                                  |
# ------------------------------------------------------------------------------

# CORE: Do not change these lines
# ----------------------------------------------------------------
POST_TITLE="${@:2:$(($#-1))}"
POST_NAME="$(echo ${@:2:$(($#-1))} | sed -e 's/ /-/g' | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/")"
CURRENT_DATE="$(date -u +'%Y-%m-%d')"
TIME=$(date -u +"%T")
FILE_NAME="${CURRENT_DATE}-${POST_NAME}.md"
# ----------------------------------------------------------------


# SETTINGS: your configuration goes here
# ----------------------------------------------------------------

# Set your destination folder
BINPATH=$(cd `dirname $0`; pwd)
POSTPATH="${BINPATH}/_drafts"
DIST_FOLDER="$POSTPATH"

# Set your blog URL
BLOG_URL="https://www.rossener.com/"

# Set your assets URL
ASSETS_URL="assets/img/"
# ----------------------------------------------------------------



# ------------------------------------------------------------------------------
# | UTILS                                                                      |
# ------------------------------------------------------------------------------

# Header logging
e_header() {
    printf "$(tput setaf 38)→ %s$(tput sgr0)\n" "$@"
}

# Success logging
e_success() {
    printf "$(tput setaf 76)✔ %s$(tput sgr0)\n" "$@"
}

# Error logging
e_error() {
    printf "$(tput setaf 1)✖ %s$(tput sgr0)\n" "$@"
}

# Warning logging
e_warning() {
    printf "$(tput setaf 3)! %s$(tput sgr0)\n" "$@"
}



# ------------------------------------------------------------------------------
# | MAIN FUNCTIONS                                                             |
# ------------------------------------------------------------------------------

# Everybody need some help
initdraft_help() {

cat <<EOT
------------------------------------------------------------------------------
INIT DRAFT - A shortcut to create an initial structure for my drafts.
------------------------------------------------------------------------------
Usage: ./initdraft.sh [options] <post name>
Options:
  -h, --help        output instructions
  -c, --create      create post
Example:
  ./initdraft.sh -c How to replace strings with sed
Important Notes:
  - This script was created to generate new text files to my blog.
Copyright (c) Thiago Rossener
Licensed under the MIT license.
------------------------------------------------------------------------------
EOT

}

# Initial Content
initdraft_content() {

echo "---"
echo "layout: post"
echo "title: \"${POST_TITLE}\""
echo "date: ${CURRENT_DATE} ${TIME}"
echo "image: ''"
echo "description:"
echo "category: ''"
echo "tags:"
echo "twitter_text:"
echo "introduction:"
echo "---"

}

# Create file
initdraft_file() {
    if [ ! -f "$FILE_NAME" ]; then
        e_header "Creating template..."
        initdraft_content > "${DIST_FOLDER}/${FILE_NAME}"
        e_success "Initial draft successfully created!"
    else
        e_warning "File already exist."
        exit 1
    fi

}



# ------------------------------------------------------------------------------
# | INITIALIZE PROGRAM                                                         |
# ------------------------------------------------------------------------------

main() {

    # Show help
    if [[ "${1}" == "-h" || "${1}" == "--help" ]]; then
        initdraft_help ${1}
        exit
    fi

    # Create
    if [[ "${1}" == "-c" || "${1}" == "--create" ]]; then
        initdraft_file $*
        exit
    fi

}

# Initialize
main $*
